package com.gitee.booting.dspi.example.consumer;

import com.gitee.booting.dspi.Extensions;
import com.gitee.booting.dspi.example.api.IEcho;
import com.gitee.booting.dspi.example.api.IHello;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@RestController
@SpringCloudApplication
public class DspiConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DspiConsumerApplication.class, args);
    }

    @GetMapping("")
    public String hello(@RequestParam String name) {
        return Extensions.get(IHello.class, "example-provider", name).hello();
    }

    @GetMapping("/echo")
    public String echo(@RequestParam String name) {
        return Extensions.get(IEcho.class, "example-provider", name).echo();
    }

    @GetMapping("/metadata")
    public Object metadata(@RequestParam String name) {
        return Extensions.get(IHello.class, "example-provider", name).metadata();
    }

    @GetMapping(value = "/metadata", params = {"who"})
    public Object metadata(@RequestParam String name, @RequestParam String who) {
        return Extensions.get(IHello.class, "example-provider", name).metadata(Collections.singletonMap("who", who));
    }
}
